<?php

class CpgaleriesController extends Core_Controller_Action 
{
	private $_galeries;
	private $_photos;
	
	public function init() {
		$this->_galeries = new Galeries();
		$this->_photos = new Photos();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cpgaleries' );
	}
	
	public function itemAction() {
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );

		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			if( empty( $post['name'] ) && !$id ) {
				$this->view->err_name = 'Podaj nazwe';
			} else {
				
				if( $id ) {
					$item = $this->_galeries->getItem( $id )->getFirst();
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$item->{$key} = $value;
						}
					}
					$item->save();

					$this->_redirect( 'cpgaleries/itemphotos/id/' . $id );
				} else {
					foreach( $post as $key => $value ) {
	
						if( strlen( $value ) > 0 ) {
							$this->_galeries->{$key} = $value;
						}
					}
					
					$this->_galeries->save();
					$this->_redirect( 'cpgaleries/itemphotos/id/' . $this->_galeries->g_id );
				}
			}

		}
		
		$this->assignType( 0 );
	}
	
	public function itemphotosAction() {
		$this->view->fileTypes = '"*.jpg"';
		$this->view->fileTypesDescription = 'Tylko zdjęcia';
		$this->view->uploadSuccess = 'uploadSuccessGallery';
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();
			foreach( $post['orders'] as $key => $value ) {
				$Photos = new Photos();

				$item = $Photos->getItem( $value )->getFirst();
				
				$item->description_pl = $post['description_pl'][$value];
				$item->description_en = $post['description_en'][$value];
				$item->description_cs = $post['description_cs'][$value];
				$item->order_item = $key;

				$item->save();
			}
		}
		
		if( $id ) {
			$this->view->list = $this->_photos->getList( $id )->toArray();
			
			$item = $this->_galeries->getItem( $id )->getFirst();
			$this->view->g_id = $item['g_id'];
			$this->view->name = $item['name'];
			
		} else {
			$this->_redirect( 'cpgaleries/list/oper/3' );
		}
		
		$this->assignType( 0 );
	}
	
	public function resizeAction()
	{
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$dir = opendir(getcwd() . $this->config['dirs']['upload']);
			while(false !== ($file = readdir($dir)))
  				if($file != '.' && $file != '..' && substr($file,strlen($file)-3,3) == 'jpg' && (substr($file,strlen($file)-8,1) != '_' &&  substr($file,strlen($file)-7,1) != '_') && substr($file,0,11) != 'testoweLove')
  				{
	  				echo $file . '<br />';
	  				$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $file );
					$images->thumb( 220, 220 );
	  				$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $file );
					$images->thumb( 100, 100 );
  				}
		
		
	}
	
	public function generateAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$post = $this->_request->getPost();
		
		$name = ( isSet( $post['name'] ) && !empty( $post['name'] ) ) ? $post['name'] : 'Galeria ' . Date( 'Y-m-d H:i:s' );
		
		$ajax = intval( $this->_request->getParam( 'ajax', 0 ) );
		
		$post['id'] = intval( $post['id'] );

		$first = false;
		
		if( $ajax && $post['id'] < 1 ) {
			$Galeries = new Galeries();
			
			$Galeries->name = $name;
			
			$Galeries->save();
			
			$post['id'] = $Galeries->g_id;
			
			$first = true;
		}

		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 643, 430 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 293, 220 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 195, 167 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 170, 170 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 112, 114 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 117, 104 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 100, 100 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 91, 61 );

		$this->_photos->galeries_g_id = $post['id'];
		$this->_photos->iden = $post['img'];
		
		if( $first ) {
			$this->_photos->main = 'yes';
		}
		
		$this->_photos->save();

		echo Zend_Json::encode(array(
			'status' => 'OK',
			'main' => ( $first ) ? 'yes' : 'no',
			'id' => $this->_photos->ph_id,
			'galleryId' => $post['id'],
			'img' => $this->view->File( $post['img'], 112 ),
			'name' => $name
		));
	}
	
	public function setglAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$g_id = intval( $this->_request->getParam( 'g_id', 0 ) );

		if( $id && $g_id ) {
			$main = $this->_photos->getMainItem( $g_id )->getFirst();
			if( $main ) {
				$main->main = 'no';
				$main->save();
			}
			
			$new = $this->_photos->getItem( $id )->getFirst();
			$new->main = 'yes';
			$new->save();
			
			$this->_redirect( 'cpgaleries/itemphotos/id/' . $g_id . '/oper/2' );
		} else {
			$this->_redirect( 'cpgaleries/list/oper/3' );
		}
	}
	
	public function listAction() {
		$page = intval( $this->_request->getParam( 'page', 1 ) );

		$pagerLayout = $this->_galeries->getListWithPager( $page, $this->view->baseUrl() );

		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;

		$this->assignType( 0 );
	}
	
	public function deleteAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$this->_galeries->deleteItem( $id );
			
			$this->_redirect( 'cpgaleries/list/oper/4' );
		} else {
			$this->_redirect( 'cpgaleries/list/oper/3' );
		}
	}
	
	public function deleteitemAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$g_id = intval( $this->_request->getParam( 'g_id', 0 ) );
		
		if( $id ) {
			$this->_photos->deleteItem( $id );

			$this->_redirect( 'cpgaleries/itemphotos/id/' . $g_id . '/oper/4' );
		} else {
			$this->_redirect( 'cpgaleries/itemphotos/id/' . $g_id . '/oper/3' );
		}
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 0 :
				$this->view->yih2 = 'Galerie';
				break;
		}
	}
}